package com.aramis.mydytt.ui.user.act

import android.app.Service
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.os.Vibrator
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.aramis.mydytt.R
import com.aramis.mydytt.ui.base.BaseActivity
import com.aramis.mydytt.ui.common.CommonMovieItemAdapter
import com.aramis.mydytt.ui.detail.MovieDetailActivity
import com.aramis.mydytt.widget.BottomDeleteView
import com.aramis.mydytt.widget.LoadingStateView
import com.aramis.mydytt.widget.TitleBar


/**
 * Description: UserFavoriteActivity
 * "喜欢"列表
 * @author lizhidan
 * Created: 2021/6/4
 */
class UserFavoriteActivity : BaseActivity() {
    companion object {
        private const val KEY_STYLE = "style"
        const val STYLE_FAVORITE = 1
        const val STYLE_DOWNLOADED = 2

        fun start(context: Context, style: Int) {
            val intent = Intent(context, UserFavoriteActivity::class.java)
            intent.putExtra(KEY_STYLE, style)
            context.startActivity(intent)
        }
    }

    private lateinit var viewModel: UserFavoriteViewModel
    private var style = STYLE_FAVORITE
    private val adapter = CommonMovieItemAdapter()
    private lateinit var loadingView: LoadingStateView
    private lateinit var titleView: TitleBar
    private lateinit var bottomDeleteView: BottomDeleteView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_user_facorite)
        initDataFromIntent()
        initView()
        observerData()
        requestData(style)
    }

    private fun observerData() {
        viewModel.movieDbLiveData.observe(this, {
            if (it == null || it.isEmpty()) {
                loadingView.showEmpty(R.string.empty_favorite)
            }
            adapter.setData(it)
        })
        viewModel.deleteDataLiveData.observe(this, {
            if (it != null) {
                //删除成功
                adapter.setData(it)
            } else {
                //删除失败
                Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show()
            }
        })
    }

    private fun requestData(style: Int) {
        actionBar?.setBackgroundDrawable(ColorDrawable(Color.parseColor("#222222")))
        if (style == STYLE_DOWNLOADED) {
            viewModel.selectDownloaded()
            title = "我下载的"
            titleColor
        } else {
            viewModel.selectFavorite()
            title = "我喜欢的"
        }
    }

    private fun initView() {
        viewModel = createViewModel()
        titleView = findViewById(R.id.view_title)
        val listView: RecyclerView = findViewById(R.id.list_user_favorite)
        listView.layoutManager = GridLayoutManager(this, 3)
        listView.adapter = adapter
        loadingView = findViewById(R.id.view_loading)
        adapter.onItemClickListener = {
            MovieDetailActivity.start(this, it)
        }

        adapter.onItemLongClickListener = {
            openSelectMode()
        }
        adapter.onItemSelectClickListener = {
            adapter.notifyItemChanged(it, 0)
        }

        titleView.setTitle(getString(if (style == STYLE_DOWNLOADED) R.string.title_activity_downloaded else R.string.title_activity_favorite))
        titleView.setRightText(R.string.cancel)
        titleView.setOnRightTextOnClickListener {
            closeSelectMode()
        }

        bottomDeleteView = findViewById(R.id.view_bottom_delete)
        bottomDeleteView.setAllSelectClickListener {
            viewModel.markAllSelect(it, adapter)
        }
        bottomDeleteView.setOnDeleteClickListener {
            closeSelectModeAndDelete()
        }
    }

    private fun openSelectMode() {
        vibrate()
        adapter.openSelectMode()
        titleView.setRightTextViewVisibility(View.VISIBLE)
        bottomDeleteView.visibility = View.VISIBLE
        val animation = AnimationUtils.loadAnimation(this, R.anim.ani_bottom_delete_in)
        bottomDeleteView.startAnimation(animation)
    }

    private fun closeSelectMode() {
        adapter.closeSelectMode()
        titleView.setRightTextViewVisibility(View.GONE)
        val animation = AnimationUtils.loadAnimation(this, R.anim.ani_bottom_delete_out)
        bottomDeleteView.startAnimation(animation)
        animation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                bottomDeleteView.visibility = View.GONE
            }

            override fun onAnimationRepeat(animation: Animation?) {
            }

        })
    }

    private fun closeSelectModeAndDelete() {
        closeSelectMode()
        viewModel.deleteData(style, adapter.dataList)
    }

    private fun initDataFromIntent() {
        style = intent.getIntExtra(KEY_STYLE, STYLE_FAVORITE)
    }

    private fun vibrate() {
        val vb = getSystemService(Service.VIBRATOR_SERVICE) as Vibrator
        vb.vibrate(150L)
    }
}